package page.sample;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import page.MyFirstWebPage;
import dao.impl.UserDao;
import entity.User;

public class Autorization extends WebPage {

	public Autorization() {
		super();
		// create FORM component which required to POST data from client->server
		final User user = new User();
		final UserDao userDao = new UserDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		user.setName("");
		user.setLastname("");
		Form<User> form = new Form("form",
				new CompoundPropertyModel<User>(user));
		form.add(new TextField<String>("login"));
		form.add(new PasswordTextField("password"));
		form.add(new SubmitLink("enter") {
			@Override
			public void onSubmit() {
				if (userDao.get(user.getLogin()) != null
						&& userDao.get(user.getLogin()).getPassword()
								.equals(user.getPassword())) {
					
					user.setName(userDao.get(user.getLogin()).getName());
					user.setLastname(userDao.get(user.getLogin()).getLastname());
					user.setTickets(userDao.get(user.getLogin()).getTickets());
					
					setResponsePage(new PrivateOfficePage(user));
				}
			}
		});
		
		form.add(new Link("registration") {
			@Override
			public void onClick() {
				setResponsePage(new RegistrationPage());
			}
		});
		
		add(form);

	}
}
